package band.wukong.practice.javase.core.tips;

import java.util.Arrays;

/**
 * Desc:
 * Author: sunjs
 * Date: 2009-11-13 14:14:03
 */
public class TestEncoding {
    public static void main(String[] args) throws Exception {

        diffEncode();
    }

    /**
     * 中文字符
     */
    public static void aboutCNString() {
        String s1 = "hi";
        String s2 = "你好";

        //Java中1个char类型的变量可存储任意编码的1个字符(1个ASC码和或1个中文字符)
        //例如：含有2个ASC和含有2个汉字字符的字符串长度是一样的
        System.out.println("s1.length() = " + s1.length());
        System.out.println("s2.length() = " + s2.length());

        //但上述两个字符串所占的byte是不一样的，前者是3，后者是5（1个汉字2byte）
        System.out.println("s1.getBytes().length = " + s1.getBytes().length);
        System.out.println("s2.getBytes().length = " + s2.getBytes().length);

        System.out.println("Arrays.toString(s1.getBytes()) = " + Arrays.toString(s1.getBytes()));
        System.out.println("Arrays.toString(s2.getBytes()) = " + Arrays.toString(s2.getBytes()));
    }

    /**
     * 不同的字符编码
     * @throws Exception
     */
    public static void diffEncode() throws Exception {
        String s = "道可道，非常道，名可名，非常名。";
        byte[] bs1 = s.getBytes("GBK");
        String s1 = new String(bs1, "Big5");
        System.out.println(s1);

        byte[] bs2 = s1.getBytes("Big5");
        String s2 = new String(bs2, "GBK");
        System.out.println(s2);

        byte[] bs3 = s.getBytes("UTF-8");
        String s3 = new String(bs3, "UTF-8");
        System.out.println(s3);

        System.out.println("Arrays.toString(bs1) = " + Arrays.toString(bs1));
        System.out.println("Arrays.toString(bs3) = " + Arrays.toString(bs3));
    }
}
